package com.hgkx.controller;

import com.hgkx.domain.UploadFile;
import com.hgkx.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传
 */
@RestController
@RequestMapping("/uploadFile")
public class UploadFileController {

    @Autowired
    UploadFileService uploadFileService;

    /**
     * 文件上传
     * @param uploadFile
     * @param req
     * @return
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest req){
        String path=null;
        try {
            path=uploadFileService.upload(uploadFile , req);
        }catch (Exception e){
            return null;
        }
        return path;
    }

    /**
     * 多文件上传
     * @param uploadFiles
     * @param req
     * @return
     */
    @RequestMapping("/uploads")
    public List<String> uploads(MultipartFile[] uploadFiles,HttpServletRequest req){
        List<String> list=new ArrayList<>();
        try {
            list=uploadFileService.uploads(uploadFiles , req);
        }catch (Exception e){
            return null;
        }
        return list;
    }
}
